var stream = require('stream');
var util = require('util');
util.inherits(MyReadable, stream.Readable);

function MyReadable(options) {
  stream.Readable.call(this, options);
  this._index = 0;
  this.tem = 0;
}
MyReadable.prototype._read = function () {
  this.push('黄鹏a');
  this.push(null);
};
var myReadable = new MyReadable({
  highWaterMark: 2
});
util.inherits(MyWriter, stream.Writable);
function MyWriter(opt) {
  stream.Writable.call(this, opt);
}
MyWriter.prototype._write = function (chunk, encoding, callback) {
  stock.push(chunk.toString('utf8'));
  callback();
  
};
var myWriter = new MyWriter();
let stock = [];


function eventMethods() {
  myReadable.on('data', function (data) {
    var flag = myWriter.write(data);
    if (!flag)
      myReadable.pause();
  });
  myReadable.on('end', function (data) {
    // console.log("读完了");
    myWriter.end();
  });

  // 缓冲区的文件，消耗完触发的事件
  myWriter.on('drain', function () {
    myReadable.resume();
  });
  myWriter.on('finish', () => {
    console.log(stock, 'finish')
  })
}
// eventMethods()

// pipeMethods()

// tansformMethods()

function pipeMethods() {
    myReadable.pipe(myWriter)
    myWriter.on('finish', () => {
      console.log(stock, 'finish')
    })
}

function tansformMethods() {


  const upperCase = new stream.Transform({
    transform(chunk, encoding, callback) {
      this.push(chunk.toString('base64'));
      callback();
    }
  });
  myReadable.pipe(upperCase).pipe(myWriter);
  myWriter.on('finish', (data) => {
    console.log(stock)
  })
}